package com.weiweiqin.controller.back;

import java.text.SimpleDateFormat;
import java.util.*;

import com.weiweiqin.model.Brands;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.weiweiqin.model.Customer;
import com.weiweiqin.service.CustomerService;
import com.weiweiqin.vo.common.CustomerTabVO;
import com.weiweiqin.vo.common.Page;
import com.weiweiqin.vo.common.Result;

@Controller
@RequestMapping("admin/customer")
public class CustomerController {

	private static final Logger logger = LoggerFactory
			.getLogger(CustomerController.class);

	@Autowired
	private CustomerService customerService;

	
	@RequestMapping(value = "/pageList.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String pageList(String email,String mobile,int currPage){
		logger.info("Enter 用户列表");
		Result result = new Result();
		Page page = new Page();
		page.setCurrPage(currPage);
		Map<String,Object> conditions = new HashMap<String, Object>();
		if(!StringUtils.isEmpty(email)){
			conditions.put("email", email);
		}
		
		if(!StringUtils.isEmpty(mobile)){
			conditions.put("mobile", mobile);
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		List<Customer> customerList = customerService.pageList(page, conditions);
		
		List<CustomerTabVO> voList = new ArrayList<CustomerTabVO>();
		if(null != customerList && !customerList.isEmpty()){
			for (Customer customer : customerList) {
				CustomerTabVO vo = new CustomerTabVO();
				vo.setCustomer(customer);
				String format = sdf.format(customer.getRegDate());
				vo.setRegDate(format);
				voList.add(vo);
			}
		}
		int totalCount = customerService.totalCount(conditions);
		page.setTotalCount(totalCount);
		Map<String,Object> mp = new HashMap<String,Object>();
		mp.put("totalCount",page.getTotalCount());
		mp.put("list", voList);
		result.setObj(mp);
		result.setResult(true);
		return result.toJson();
	}

	@RequestMapping(value = "/setHard.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String setHard(int id,int hard) {
		logger.info("Enter method setHard...");
		Result result = new Result();
		try {
			Customer customer = customerService.get(id);
			customer.setHard(hard);
			customerService.update(customer);
		} catch (Exception e) {
			logger.error("数据库异常...", e);
			result.setResult(false);
			result.setErrorMsg("数据库异常。。。");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}

	@RequestMapping(value = "/get.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String get(int id) {
		Result result = new Result();
		try {
			Customer customer = customerService.get(id);
			result.setObj(customer);
		} catch (Exception e) {
			logger.error("数据库异常...", e);
			result.setResult(false);
			result.setErrorMsg("数据库异常。。。");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}
}
